package com.basic.auth.controller;


import com.basic.auth.controller.req.AuthRoleFuncReqForm;
import com.basic.auth.controller.res.AuthFuncResForm;
import com.basic.auth.controller.res.AuthRoleFuncResForm;
import com.basic.auth.dao.model.AuthRoleFunc;
import com.basic.auth.domain.AuthFuncBo;
import com.basic.auth.domain.AuthRoleFuncBo;
import com.basic.auth.service.AuthRoleFuncService;
import com.basic.common.bean.BeanCopierDsUtil;
import com.basic.common.bean.BeanCopyDsUtil;
import com.basic.common.constant.CommonDsConstant;
import com.basic.common.result.Errors;
import com.basic.common.result.WebResult;
import com.basic.common.string.StringCheckDsUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 角色功能管理controller
 *
 * @author 
 * @version 1.0
 * @Date  18:32
 */
@RestController
@RequestMapping(value = "/roleFunc")
public class AuthRoleFuncController {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AuthRoleFuncService authRoleFuncService;

     @RequestMapping(value = "/isCanUse", method = RequestMethod.POST)
    public WebResult isCanUse(@RequestBody AuthRoleFuncReqForm reqForm, BindingResult result) {
//        ApiResult webAuthResult = ApiAuthVerification.getResult(reqForm, result);
//        if (!webAuthResult.isSuccess) {
//            return webAuthResult.getWebResult();
//        }
        AuthRoleFuncBo roleFuncBo = BeanCopierDsUtil.copyProperties(reqForm, AuthRoleFuncBo.class);
        List<AuthRoleFunc> userFuncList = authRoleFuncService.selRoleFuncListByBoId(roleFuncBo);
        if (CollectionUtils.isEmpty(userFuncList)) {
            return WebResult.success(CommonDsConstant.DEFAULT_INT_NO);
        }
        return WebResult.success(CommonDsConstant.DEFAULT_INT_YES);
    }


    /**
     * 为角色添加后台和其他平台功能
     *
     * @param roleFuncReqForm
     * @return void
     * @author
     * @date 17:45 
     */
    @RequestMapping(value = "/addHtAndOrderFuncList", method = RequestMethod.POST)
    public WebResult addHtAndOrderFuncList( @RequestBody AuthRoleFuncReqForm roleFuncReqForm) {
        if (roleFuncReqForm == null
                || StringCheckDsUtil.isBlank(roleFuncReqForm.getRoleCode())
                || roleFuncReqForm.getSystemId() == null
                || roleFuncReqForm.getSubSystemId() == null || Strings.isBlank(roleFuncReqForm.getFuncIds())
        ) {
            WebResult result = new WebResult();
            result.setCode("1");
            result.setMsg("增加角色参数缺失");
            result.setException(Errors.SYSTEM_REQUEST_PARAM_ERROR.exception);
            return result;
        }
        AuthRoleFuncBo roleFuncBo = new AuthRoleFuncBo();
        BeanCopyDsUtil.copyProperties(roleFuncReqForm, roleFuncBo);
        List<AuthFuncBo> authFuncBoList = BeanCopierDsUtil.copyPropertiesOfList(roleFuncReqForm.getFuncList(), AuthFuncBo.class);
        roleFuncBo.setFuncList(authFuncBoList);
        authRoleFuncService.addHtAndOrderFuncList(roleFuncBo);
        return WebResult.success();
    }

    /**
     * 根据角色id查询角色功能
     * @param roleFuncReqForm
     * @return
     */
    @RequestMapping(value = "/selListByRoleSysId", method = RequestMethod.POST)
    public WebResult selListByRoleSysId( @RequestBody AuthRoleFuncReqForm roleFuncReqForm) {
        AuthRoleFuncBo roleFuncBo = new AuthRoleFuncBo();
        BeanCopyDsUtil.copyProperties(roleFuncReqForm, roleFuncBo);
        List<AuthRoleFuncBo> roleFuncList = authRoleFuncService.selListByRoleSysId(roleFuncBo);
        List<AuthRoleFuncResForm> roleFuncResFormList = modelToResList(roleFuncList);
        return WebResult.success(roleFuncResFormList);
    }

    /**
     * 根据用户boid查询用户拥有的功能
     *
     * @param roleFuncReqForm
     * @param result
     * @return com.nxin.etpojar.common.result.WebResult
     * @author 
     * @since  17:41
     */
//    @ApiOperation(value = "查询角色功能", notes = "查询参数")
//    @ApiImplicitParam(name = "AuthRoleFuncReqForm ", value = "查询参数", required = true, dataType = "AuthRoleFuncReqForm ")
    @RequestMapping(value = "/selListByBoId", method = RequestMethod.POST)
    public WebResult selListByBoId(@RequestBody AuthRoleFuncReqForm roleFuncReqForm, BindingResult result) {
//        ApiResult webAuthResult = ApiAuthVerification.getResult(roleFuncReqForm, result);
//        if (!webAuthResult.isSuccess) {
//            return webAuthResult.getWebResult();
//        }
        AuthRoleFuncBo roleFuncBo = new AuthRoleFuncBo();
        BeanCopyDsUtil.copyProperties(roleFuncReqForm, roleFuncBo);
        List<AuthFuncBo> authFuncBos = authRoleFuncService.selListByBoId(roleFuncBo);
        List<AuthFuncResForm> authFuncResForms = BeanCopierDsUtil.copyPropertiesOfList(authFuncBos, AuthFuncResForm.class);
        if (CollectionUtils.isNotEmpty(authFuncResForms)) {
            for (int i = 0; i < authFuncResForms.size(); i++) {
                List<AuthFuncBo> subFuncList = authFuncBos.get(i).getSubFuncList();
                List<AuthFuncResForm> authFuncResForms1 = BeanCopierDsUtil.copyPropertiesOfList(subFuncList, AuthFuncResForm.class);
                authFuncResForms.get(i).setSubFuncList(authFuncResForms1);
            }
        }
        return WebResult.success(authFuncResForms);
    }

//    @ApiOperation(value = "查询系统下角色功能", notes = "查询参数")
//    @ApiImplicitParam(name = "AuthRoleFuncReqForm ", value = "查询参数", required = true, dataType = "AuthRoleFuncReqForm ")
    @RequestMapping(value = "/selListBySystemId", method = RequestMethod.POST)
    public WebResult selListBySystemId(@RequestBody AuthRoleFuncReqForm roleFuncReqForm, BindingResult result) {
//        ApiResult webAuthResult = ApiAuthVerification.getResult(roleFuncReqForm, result);
//        if (!webAuthResult.isSuccess) {
//            return webAuthResult.getWebResult();
//        }
        AuthRoleFuncBo roleFuncBo = new AuthRoleFuncBo();
        BeanCopyDsUtil.copyProperties(roleFuncReqForm, roleFuncBo);
        List<AuthFuncBo> authFuncBos = authRoleFuncService.selListBySystemId(roleFuncBo);
        List<AuthFuncResForm> authFuncResForms = BeanCopierDsUtil.copyPropertiesOfList(authFuncBos, AuthFuncResForm.class);
        if (CollectionUtils.isNotEmpty(authFuncResForms)) {
            for (int i = 0; i < authFuncResForms.size(); i++) {
                List<AuthFuncBo> subFuncList = authFuncBos.get(i).getSubFuncList();
                List<AuthFuncResForm> authFuncResForms1 = BeanCopierDsUtil.copyPropertiesOfList(subFuncList, AuthFuncResForm.class);
                authFuncResForms.get(i).setSubFuncList(authFuncResForms1);
            }
        }
        return WebResult.success(authFuncResForms);
    }

//    @ApiOperation(value = "添加角色功能", notes = "添加参数")
//    @ApiImplicitParam(name = "AuthRoleFuncReqForm ", value = "添加参数", required = true, dataType = "AuthRoleFuncReqForm ")
    @RequestMapping(value = "/addAuthRoleFunc", method = RequestMethod.POST)
    public WebResult addAuthRoleFunc( @RequestBody AuthRoleFuncReqForm roleFuncReqForm, BindingResult result) {
//        ApiResult webAuthResult = ApiAuthVerification.getResult(roleFuncReqForm, result);
//        if (!webAuthResult.isSuccess) {
//            return webAuthResult.getWebResult();
//        }
        AuthRoleFuncBo roleFuncBo = new AuthRoleFuncBo();
        BeanCopyDsUtil.copyProperties(roleFuncReqForm, roleFuncBo);
        authRoleFuncService.addAuthRoleFunc(roleFuncBo);
        return WebResult.success();
    }

    private List<AuthRoleFuncResForm> modelToResList(List<AuthRoleFuncBo> roleFuncList) {
        if (CollectionUtils.isEmpty(roleFuncList)) {
            return null;
        }
        List<AuthRoleFuncResForm> resFormList = new ArrayList<AuthRoleFuncResForm>();
        AuthRoleFuncResForm resForm = null;
        for (AuthRoleFuncBo roleFunc : roleFuncList) {
            resForm = modelToRes(roleFunc);
            resFormList.add(resForm);
        }
        return resFormList;
    }

    private AuthRoleFuncResForm modelToRes(AuthRoleFuncBo roleFunc) {
        if (roleFunc == null) {
            return null;
        }
        AuthRoleFuncResForm resForm = BeanCopierDsUtil.copyProperties(roleFunc, AuthRoleFuncResForm.class);
        return resForm;
    }
}
